package com.zaofan.utils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.zaofan.bean.Account;
import com.zaofan.bean.Goods;
import com.zaofan.bean.GoodsOrderInfo;
import com.zaofan.bean.OrderInfo;
import com.zaofan.bean.Prepaid;
import com.zaofan.bean.Product;
import com.zaofan.bean.Recharge;
import com.zaofan.bean.Shop;
import com.zaofan.bean.User;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * 极光推送工具类
 * 
 * @author dxt
 *
 */
public class JPushUtils {

	public static final String APP_KEY = "1d4bc154b1990a65fc0a42c4";
	public static final String APP_SECRET = "5805a551cb858e7f8b80c8d6";
	public static final JPushClient jpushClient;
	public static Gson gson = new Gson();

	static {
		jpushClient = new JPushClient(APP_SECRET, APP_KEY, 3);
	}

	public static void sendOrderSuccess(OrderInfo order, Account account) {
		try {
			String content = "您的订单【" + order.getOrderNo() + "】已经生效,吃早餐,要有范!，早范君将会在明日上午将您的美味早餐配送至您指定地点!!";
			PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
					.setAudience(Audience.alias(account.getMobile()))
					.setNotification(Notification.newBuilder().setAlert(content)
							.addPlatformNotification(
									IosNotification.newBuilder().setBadge(1).setSound("default").build())
							.build())
					.setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
			PushResult result = jpushClient.sendPush(payload);
			System.err.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

	public static void sendCompleteOrder(OrderInfo order, String mobile) {
		try {
			String content = "您的订单【" + order.getOrderNo() + "】已为您送到,请享受早范为您提供的美味!!";
			PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
					.setAudience(Audience.alias(mobile))
					.setNotification(Notification.newBuilder().setAlert(content)
							.addPlatformNotification(
									IosNotification.newBuilder().setBadge(1).setSound("default").build())
							.build())
					.setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
			PushResult result = jpushClient.sendPush(payload);
			System.err.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

	public static void sendOrderSuccess(Account account) {
		try {
			String content = "您的周套餐,已经生效,吃早餐,要有范!，早范君将会在指定的时间将您的美味早餐配送至您指定地点!!";
			PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
					.setAudience(Audience.alias(account.getMobile()))
					.setNotification(Notification.newBuilder().setAlert(content)
							.addPlatformNotification(
									IosNotification.newBuilder().setBadge(1).setSound("default").build())
							.build())
					.setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
			PushResult result = jpushClient.sendPush(payload);
			System.err.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

	public static void sendOrderFail(OrderInfo order, Account account) {
		try {
			String content = "您的订单【" + order.getOrderNo() + "】即将过期了,赶紧去支付下单!!";
			PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
					.setAudience(Audience.alias(account.getMobile()))
					.setNotification(Notification.newBuilder().setAlert(content)
							.addPlatformNotification(
									IosNotification.newBuilder().setBadge(1).setSound("default").build())
							.build())
					.setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
			PushResult result = jpushClient.sendPush(payload);
			System.err.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

	public static void sendProduct(Product product, Shop shop) {
		try {
			String content = "商铺【" + shop.getShopName() + "】,推出了美味的" + product.getName() + ",美味价格只需:"
					+ product.getPrice() + ",欢迎大家来品尝。";
			Map<String, String> extras = new HashMap<>();
			extras.put("shopId", gson.toJson(shop));
			PushPayload android = PushPayload.newBuilder().setPlatform(Platform.all())
					.setNotification(
							Notification.newBuilder().setAlert(content)
									.addPlatformNotification(AndroidNotification.newBuilder().setAlert(content)
											.addExtras(extras).build())
					.addPlatformNotification(
							IosNotification.newBuilder().setBadge(1).setSound("default").addExtras(extras).build())
					.build()).setAudience(Audience.all())
					.setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
			PushResult result = jpushClient.sendPush(android);
			System.err.println("Got result - android - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

	public static void sendGoods(Goods goods) {
		try {
			String content = "早范为您提供了实惠使用的电器:" + goods.getName() + ",赶紧去看看吧!!";
			PushPayload android = PushPayload.newBuilder().setPlatform(Platform.all())
					.setNotification(Notification.newBuilder().setAlert(content)
							.addPlatformNotification(AndroidNotification.newBuilder().setAlert(content).build())
							.addPlatformNotification(
									IosNotification.newBuilder().setBadge(1).setSound("default").build())
							.build())
					.setAudience(Audience.all()).setOptions(Options.newBuilder().setApnsProduction(false).build())
					.build();
			PushResult result = jpushClient.sendPush(android);
			System.err.println("Got result - android - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

	public static void sendNotice(String content) {
		try {
			PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.all())
					.setNotification(Notification.newBuilder().setAlert(content)
							.addPlatformNotification(
									IosNotification.newBuilder().setBadge(1).setSound("default").build())
							.build())
					.setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
			PushResult result = jpushClient.sendPush(payload);
			System.err.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Account account = new Account();
		account.setMobile("18900007777");
		Prepaid prepaid = new Prepaid();
		prepaid.setCard("101012");
		prepaid.setMoney(new BigDecimal(50));
		sendPrepaidSuccess(prepaid, account);
	}

	public static void sendRechargeSuccess(Recharge recharge, Account account) {
		try {
			String content = "恭喜您,成功充值：" + recharge.getMoney() + ",吃早餐,要有范!感谢您的使用~~";
			PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
					.setAudience(Audience.alias(account.getMobile()))
					.setNotification(Notification.newBuilder().setAlert(content)
							.addPlatformNotification(
									IosNotification.newBuilder().setBadge(1).setSound("default").build())
							.build())
					.setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
			PushResult result = jpushClient.sendPush(payload);
			System.err.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

	public static void sendPrepaidSuccess(Prepaid prepaid, Account account) {
		try {
			String content = "恭喜您,卡号:" + prepaid.getCard() + ",使用成功,成功充值：" + prepaid.getMoney() + ",吃早餐,要有范!感谢您的使用~~";
			PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
					.setAudience(Audience.alias(account.getMobile()))
					.setNotification(Notification.newBuilder().setAlert(content)
							.addPlatformNotification(
									IosNotification.newBuilder().setBadge(1).setSound("default").build())
							.build())
					.setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
			PushResult result = jpushClient.sendPush(payload);
			System.err.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

	public static void sendGoodsOrderSuccess(GoodsOrderInfo order, Account account) {
		try {
			String content = "您的订单【" + order.getOrderNo() + "】已经支付成功!，早范君将会尽快安排送货!!";
			PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
					.setAudience(Audience.alias(account.getMobile()))
					.setNotification(Notification.newBuilder().setAlert(content)
							.addPlatformNotification(
									IosNotification.newBuilder().setBadge(1).setSound("default").build())
							.build())
					.setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
			PushResult result = jpushClient.sendPush(payload);
			System.err.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

}
